{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "restricted-homework",
   "metadata": {},
   "source": [
    "# Spanish Real State IDs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bronze-junior",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "surface-training",
   "metadata": {},
   "source": [
    "The function `clean_es_referenciacatastral()` cleans a column containing Spanish real state id (Referenciacatastral) strings, and standardizes them in a given format. The function `validate_es_referenciacatastral()` validates either a single Referenciacatastral strings, a column of Referenciacatastral strings or a DataFrame of Referenciacatastral strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lucky-copying",
   "metadata": {},
   "source": [
    "Referenciacatastral strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"4A08169P03PRAT0001LR\"\n",
    "* `standard`: Referenciacatastral strings with proper whitespace in the proper places, like \"4A08169 P03PRAT 0001 LR\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_es_referenciacatastral()` and `validate_es_referenciacatastral()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "timely-morocco",
   "metadata": {},
   "source": [
    "### An example dataset containing Referenciacatastral strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "neither-first",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"referenciacatastral\": [\n",
    "            \"4A08169P03PRAT0001LR\",\n",
    "            \"7837301/VG8173B 0001 TT\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unauthorized-referral",
   "metadata": {},
   "source": [
    "## 1. Default `clean_es_referenciacatastral`\n",
    "\n",
    "By default, `clean_es_referenciacatastral` will clean referenciacatastral strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "biblical-houston",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_es_referenciacatastral\n",
    "clean_es_referenciacatastral(df, column = \"referenciacatastral\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "municipal-heritage",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "announced-emerald",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "light-mailing",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amino-activation",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_es_referenciacatastral(df, column = \"referenciacatastral\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "nasty-negative",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sacred-acceptance",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_es_referenciacatastral(df, column = \"referenciacatastral\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "raised-evidence",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned Referenciacatastral strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "featured-morris",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_es_referenciacatastral(df, column=\"referenciacatastral\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "micro-melbourne",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "loving-starter",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "romance-dress",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_es_referenciacatastral(df, \"referenciacatastral\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "smooth-bargain",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "flexible-entry",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_es_referenciacatastral(df, \"referenciacatastral\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cross-piano",
   "metadata": {},
   "source": [
    "## 4. `validate_es_referenciacatastral()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aggressive-haiti",
   "metadata": {},
   "source": [
    "`validate_es_referenciacatastral()` returns `True` when the input is a valid Referenciacatastral. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_es_referenciacatastral()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_es_referenciacatastral()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_es_referenciacatastral()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "czech-investigator",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_es_referenciacatastral\n",
    "print(validate_es_referenciacatastral(\"4A08169P03PRAT0001LR\"))\n",
    "print(validate_es_referenciacatastral(\"7837301/VG8173B 0001 TT\"))\n",
    "print(validate_es_referenciacatastral(\"51824753556\"))\n",
    "print(validate_es_referenciacatastral(\"51 824 753 556\"))\n",
    "print(validate_es_referenciacatastral(\"hello\"))\n",
    "print(validate_es_referenciacatastral(np.nan))\n",
    "print(validate_es_referenciacatastral(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "genetic-defense",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "studied-poetry",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_es_referenciacatastral(df[\"referenciacatastral\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abroad-terminology",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dirty-thursday",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_es_referenciacatastral(df, column=\"referenciacatastral\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "controversial-brooks",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "interim-gamma",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_es_referenciacatastral(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "rubber-horizontal",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
